package cn.butcher21.view;

import cn.butcher21.utils.BuildScene;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainViewController {

    @FXML
    private Label goods;

    @FXML
    private Label pay;

    @FXML
    private Label chart;

    @FXML
    private Label user;

    @FXML
    private Pane main;

    @FXML
    private Label time;

    @FXML
    private Label who;

    @FXML
    private Label setting;



    @FXML
    private void initialize() {
        LoginController login = (LoginController) Context.rootController.get(LoginController.class.getSimpleName());
        String username = login.getUsername().getText();

        who.setText("当前用户：" + username);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                                String datetime = sdf.format(new Date());
                                time.setText(datetime);
                            }
                        });
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();

        pay.setStyle("-fx-background-color: #353b48");
        Parent sale = BuildScene.parents.get("sale");
        main.getChildren().clear();
        main.getChildren().add(sale);
    }

    @FXML
    void chartM(MouseEvent event) throws IOException {
        clearColor();
        chart.setStyle("-fx-background-color: #353b48");
        Parent chart = BuildScene.parents.get("chart");
        main.getChildren().clear();
        main.getChildren().add(chart);

    }

    @FXML
    void goodsM(MouseEvent event) throws IOException {
        clearColor();
        goods.setStyle("-fx-background-color: #353b48");
        Parent stock = BuildScene.parents.get("stock");
        main.getChildren().clear();
        main.getChildren().add(stock);
    }

    @FXML
    void payM(MouseEvent event) throws IOException {
        clearColor();
        pay.setStyle("-fx-background-color: #353b48");
        Parent sale = BuildScene.parents.get("sale");
        main.getChildren().clear();
        main.getChildren().add(sale);
    }

    @FXML
    void settingM(MouseEvent event) throws IOException {
        clearColor();
        setting.setStyle("-fx-background-color: #353b48");
        Parent setting = BuildScene.parents.get("setting");
        main.getChildren().clear();
        main.getChildren().add(setting);
    }

    @FXML
    void userM(MouseEvent event) throws IOException {
        clearColor();
        user.setStyle("-fx-background-color: #353b48");
        Parent user = BuildScene.parents.get("user");
        main.getChildren().clear();
        main.getChildren().add(user);
    }

    /**
     * 清空标签颜色
     */
    void clearColor() {
        user.setStyle("-fx-background-color: #535c68");
        goods.setStyle("-fx-background-color: #535c68");
        pay.setStyle("-fx-background-color: #535c68");
        chart.setStyle("-fx-background-color: #535c68");
        setting.setStyle("-fx-background-color: #535c68");
    }

}
